#include "piece_elephant.hpp"

bool Piece_Elephant::move(const Coordinate &coor_end) {
    Coordinate det = coor_end - coor;
    if (det * det == 8) {
        if (!pieces[gameMap[(coor + coor_end) / 2]]->isLife()) {
            if (color == RED) {
                if (coor_end.x > 4 && coor_end.x <= 9) {
                    if (pieces[gameMap[coor_end]]->isLife()) {
                        if (pieces[gameMap[coor_end]]->getColor() != color) {
                            pieces[gameMap[coor_end]]->setLife(false);
                            updateCoor(gameMap, coor_end);
                            return true;
                        }
                    } else updateCoor(gameMap, coor_end);
                    return true;
                }
                return false;
            } else {
                if (coor_end.x >= 0 && coor_end.x < 5) {
                    if (pieces[gameMap[coor_end]]->isLife()) {
                        if (pieces[gameMap[coor_end]]->getColor() != color) {
                            pieces[gameMap[coor_end]]->setLife(false);
                            updateCoor(gameMap, coor_end);
                            return true;
                        }
                    } else updateCoor(gameMap, coor_end);
                    return true;
                }
                return false;
            }
        }
    }
    return false;
}

void Piece_Elephant::init() {
    switch (pt) {
        case PieceType::RED_ELEPHANT1:
            coor.x = 9;
            coor.y = 2;
            color = RED;
            break;
        case PieceType::RED_ELEPHANT2:
            coor.x = 9;
            coor.y = 6;
            color = RED;
            break;
        case PieceType::BLACK_ELEPHANT1:
            coor.x = 0;
            coor.y = 2;
            color = BLACK;
            break;
        case PieceType::BLACK_ELEPHANT2:
            coor.x = 0;
            coor.y = 6;
            color = BLACK;
            break;
    }
    life = true;
}

Piece_Elephant::~Piece_Elephant() {};
